// 引入model的sql语句
const {
    getUsers
} = require("../../model/login");

// 引入jsonwebtoken模块
const jwt = require("jsonwebtoken");
const SECRET = "hello";

const login = async function(req,res){
    const { username,password } = req.body;

    // 密码加密

    // 判断是否存在
    let user = await getUsers();

    let isUser = user.find(item=>{
        return item.username === username;
    });
    if (isUser){
        if (password === isUser.password) {
            // 创建token
            const token = jwt.sign(isUser.username,SECRET);
            res.send({
                code: 200,
                msg: "欢迎回来！",
                username,
                token,
            })
        }else {
            res.send({
                code: 500,
                msg: "密码错误"
            })
        }
    }else {
        res.send({
            code: 500,
            msg: "用户输入错误或不存在，请联系管理员"
        })
    }


}

module.exports = login